package com.awa.usercenter.service.impl;

import com.awa.usercenter.common.BaseResponse;
import com.awa.usercenter.common.utils.ResultUtils;
import com.awa.usercenter.mapper.FeedbackMapper;
import com.awa.usercenter.model.domain.Feedback;
import com.awa.usercenter.model.domain.response.PageInfo;
import com.awa.usercenter.model.domain.response.UserNumResponse;
import com.awa.usercenter.model.domain.response.FeedbackResponse;
import com.awa.usercenter.service.FeedbackService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author zhengcan
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2024-01-23 08:40:42
*/

@Service
@Slf4j
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    // 此处可不注入mapper 因为这里的Service类本身自带save等方法
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public UserNumResponse getUserNum(){
        Long status = 1L; //已完成
        //done_feedback
        Long doneFeedbackNum = feedbackMapper.getUserNumByStatus(status);
        status = 0L; //未完成
        Long doingFeedBackNum = feedbackMapper.getUserNumByStatus(status);

        UserNumResponse userNumResponse = new UserNumResponse();
        userNumResponse.setDoing_feedback(doneFeedbackNum);
        userNumResponse.setDoing_feedback(doingFeedBackNum);
        return userNumResponse;
    }

    @Override
    public PageInfo<FeedbackResponse> getUserRecordingListPage(Long page, Long pageSize, Long status) {
        Page<FeedbackResponse> pages = new Page<>(page, pageSize);
        Page<FeedbackResponse> feedbackList = feedbackMapper.getFeedbackList(pages,status);

        PageInfo<FeedbackResponse> userRecordingPageInfo = new PageInfo<>();
        for (FeedbackResponse record : feedbackList.getRecords()) {
            String feedbackStatusStr = getFeedbackStatusStr(record.getEventStatus());
            record.setEventStatus(feedbackStatusStr);
        }
        userRecordingPageInfo.setList(feedbackList.getRecords());
        int total = (int) feedbackList.getTotal();
        userRecordingPageInfo.setCount(total);
        return userRecordingPageInfo;
    }
    public String getFeedbackStatusStr(String event){
        switch (event){
            case "0" :
                return "未完成";
            case "1":
                return "已通过";
            case "2":
                return "已忽略";
            default:
                return "无";
        }
    }

    @Override
    public BaseResponse<String> batchUpateStatus(List<String> feedbackIds, Long status) {
        // status 0 未完成 1 已通过 2 已忽略
        List<Feedback> feedbackList = feedbackMapper.selectBatchIds(feedbackIds);
        for (Feedback feedback : feedbackList) {
            String statusStr =String.valueOf(status);
            feedback.setEventStatus(statusStr);
        }
        boolean b = updateBatchById(feedbackList);
        if (b){
            return ResultUtils.success("操作成功");
        }else{
            return ResultUtils.success("操作失败");
        }
    }

}




